package adv.i31.robot;

import android.opengl.GLES20;
import android.opengl.Matrix;

public class Camera {

	private float[] mViewMatrix = new float[16];

	private float[] mProjMatrix = new float[16];

	public Camera() {
		// Position the eye behind the origin.
		final float eyeX = 0.0f;
		final float eyeY = 0.0f;
		final float eyeZ = 0.0f;

		// We are looking toward the distance
		final float lookX = 0.0f;
		final float lookY = 0.0f;
		final float lookZ = -1.0f;

		// Set our up vector. This is where our head would be pointing were we
		// holding the camera.
		final float upX = 0.0f;
		final float upY = 1.0f;
		final float upZ = 0.0f;

		// Set the view matrix. This matrix can be said to represent the camera
		// position.
		// NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination
		// of a model and
		// view matrix. In OpenGL 2, we can keep track of these matrices
		// separately if we choose.
		Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);

		Matrix.orthoM(mProjMatrix, 0, -10.0f, 10f, -10f, 10f, 1f, -10f);

		Util.print4x4Matrix(mViewMatrix);
	}

	public float[] getmProjMatrix() {
		return mProjMatrix;
	}

	public float[] getmViewMatrix() {
		return mViewMatrix;
	}

	public void onSurfaceChanged(int width, int height) {
		// Matrix.orthoM(mProjMatrix, 0, left, right, bottom, top, near, far)
		System.out.format("Camera.onSurfaceChanged w:%d  h:%d\n", width, height);

		GLES20.glViewport(0, 0, width, height);

		if (width > height) {
			// landscape
			float ratio = (float) width / (float) height;
			Matrix.orthoM(mProjMatrix, 0, -ratio * 10.0f, ratio * 10.0f, // left
																			// right
					-10.0f, 10.0f, // bottom top
					1f, -10f); // near far
		} else {
			// portrait
			float ratio = (float) height / (float) width;
			Matrix.orthoM(mProjMatrix, 0, -10.0f, 10.0f, // left right
					-ratio * 10.0f, ratio * 10.0f, // bottom top
					1f, -10f); // near far
		}
	}

	public void setmProjMatrix(float[] mProjMatrix) {
		this.mProjMatrix = mProjMatrix;
	}

	public void setmViewMatrix(float[] mViewMatrix) {
		this.mViewMatrix = mViewMatrix;
	}
}
